package 外观数列;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class Solution {

    public static void main(String[] args) {
        String s = countAndSay2(5);
        System.out.println(s);
    }
    //网友的
    public static  String countAndSay2(int n) {
        String ans = "1";
        for (int i = 2; i <= n; i++) {
            String cur = "";
            int m = ans.length();
            for (int j = 0; j < m; ) {
                int k = j + 1;
                while (k < m && ans.charAt(j) == ans.charAt(k)) k++;
                int cnt = k - j;
                cur += cnt + "" + ans.charAt(j);
                j = k;
            }
            ans = cur;
        }
        return ans;
    }
//
//    作者：AC_OIer
//    链接：https://leetcode-cn.com/problems/count-and-say/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-pqv4/
//    来源：力扣（LeetCode）
//    著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。

    //我自己的
    public static String countAndSay(int n) {

        String numStr = "";

        for (int i = 1; i <= n; i++) {
            if(i==1){
                numStr = "1";
                continue;
            }

            char[] chars = numStr.toCharArray();
            Map<Integer, Map<Character, Integer>> map = new LinkedHashMap<>();
            int kuang = 0;

            for (int j = 0; j < chars.length; j++) {
                if(j == 0){
                    kuang++;
                    Map<Character, Integer>  temp = new HashMap<>();
                    temp.put(chars[j],1);
                    map.put(kuang,temp);
                }
                else if(chars[j] == chars[j-1]){
                    Integer count = map.get(kuang).get(chars[j]);
                    map.get(kuang).put(chars[j],count+1);
                }
                else{
                    kuang++;
                    Map<Character, Integer>  temp = new HashMap<>();
                    temp.put(chars[j],1);
                    map.put(kuang,temp);
                }
            }


            StringBuffer des = new StringBuffer();
            for (Integer key1 : map.keySet()) {
                Map<Character, Integer> characterIntegerMap = map.get(key1);
                for (Character character : characterIntegerMap.keySet()) {
                    String number = character.toString();
                    int count = characterIntegerMap.get(character);
                    String str = count + number;
                    des.append(str);
                }
            }
            numStr = des.toString();

            System.out.println(numStr);

        }

        return numStr;
    }

    public static String generateNumber(int n) {



        String numStr = "";

        if(n == 1){
            numStr = "1";
        }




        return numStr;
    }
}
